from kisp import *
import random
import sys
import os

class Portage(KISP):
	Type = "Sensor"
	Name = "Portage"
	Description = "Checks Portage for Updates"
	Arguments = []
	Units = "Updates Available"

	def Start(self,arguments):
		try:
			p = os.popen('glsa-check -p affected | grep "/" -c')
			sec_updates = p.read().split("\n")[0]
			p = os.popen('emerge -Duqp world')
			all_updates = p.read()
			
			if sec_updates == "0":
				return [0, len(all_updates.split("\n"))-1,all_updates]
			else:
				return [1, len(all_updates.split("\n"))-1,all_updates]
		except:
			return [-1,0,"Plugin Failed: %s - %s" % (sys.exc_info()[0],sys.exc_info()[1])]
